package dontCare.gwtester.client.gwt.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import dontCare.gwtester.client.tool.UnitWidget;
import dontCare.gwtester.client.tool.SourceUrl;

public class AbsolutePanelTest extends UnitWidget {
	private static AbsolutePanelTestUiBinder uiBinder = GWT.create(AbsolutePanelTestUiBinder.class);
	interface AbsolutePanelTestUiBinder extends
			UiBinder<Widget, AbsolutePanelTest> {
	}
	
	//TODO 放棄使用「在 ui.xml 指定 style」的方式，程式碼看起來太囉唆了。 
	interface MyStyle extends CssResource{
		String red();
		String green();
		String blue();
		String border();
	}
	
	@UiField MyStyle style;
	@UiField AbsolutePanel panel;
	@UiField Button add;
	@UiField Button insert;
	@UiField Button reset;
	private Label first = new Label("1");
	private Label second = new Label("2");
	private Label third = new Label("3");
	
	public AbsolutePanelTest() {
		initWidget(uiBinder.createAndBindUi(this));
		panel.setPixelSize(500, 500);
		setComponent(first, style.green());
		setComponent(second, style.blue());
		setComponent(third, style.red());
		init();
	}
	
	private void setComponent(Widget c, String styleName){
		c.setPixelSize(200, 200);
		c.addStyleName(styleName);
		c.addStyleName(style.border());
	}
	
	private void init() {
		panel.clear();
		panel.add(first, 0, 0);
		panel.add(second, 100, 100);
		setButtonEnable(true);
	}

	@UiHandler("add") void clickAdd(ClickEvent e){
		panel.add(third, 50, 50);
		setButtonEnable(false);
	}
	
	@UiHandler("insert") void clickInsert(ClickEvent e){
		panel.insert(third, 50, 50, 1);
		setButtonEnable(false);
	}
	
	@UiHandler("reset") void clickReset(ClickEvent e){
		init();
	}
	
	private void setButtonEnable(boolean b) {
		add.setEnabled(b);
		insert.setEnabled(b);
		reset.setEnabled(!b);
	}

	@Override
	public List<String> getSourceUrl() {
		ArrayList<String> result = new ArrayList<String>();
		result.add(SourceUrl.genJava(AbsolutePanelTest.class));
		result.add(SourceUrl.genUiXml(AbsolutePanelTest.class));
		return result;
	}

	@Override
	public HTML getDescription() {
		return new HTML("<a href='http://google-web-toolkit.googlecode.com/svn/javadoc/2.4/com/google/gwt/user/client/ui/AbsolutePanel.html'>AbsolutePanel</a> 的行為測試");
	}

	@Override
	public String getName() {
		return "AbsolutePanel 測試";
	}
}